package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysPowersMapper;
import com.ruoyi.system.domain.SysPowers;
import com.ruoyi.system.service.ISysPowersService;

import javax.annotation.Resource;

/**
 * 职权信息Service业务层处理
 * 
 * @author ruoyi_Dzy
 * @date 2022-02-12
 */
@Service
public class SysPowersServiceImpl implements ISysPowersService
{
    @Resource
    private SysPowersMapper sysPowersMapper;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询职权信息
     * 
     * @param recNo 职权信息ID
     * @return 职权信息
     */
    @Override
    public SysPowers selectSysPowersById(String recNo)
    {
        return sysPowersMapper.selectSysPowersById(recNo);
    }

    /**
     * 查询职权信息列表
     * 
     * @param sysPowers 职权信息
     * @return 职权信息
     */
    @Override
    public List<SysPowers> selectSysPowersList(SysPowers sysPowers)
    {
        return sysPowersMapper.selectSysPowersList(sysPowers);
    }

    /**
     * 新增职权信息
     * 
     * @param sysPowers 职权信息
     * @return 结果
     */
    @Override
    public int insertSysPowers(SysPowers sysPowers)
    {
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        sysPowers.setCreateBy(sysDept.getDeptName());
        sysPowers.setCreateTime(DateUtils.getNowDate());
        UUID uuid=UUID.randomUUID();
        String[] idd=uuid.toString().split("-");
        String id=idd[0]+idd[1]+idd[2]+idd[3]+ idd[4];
        sysPowers.setRecNo(id);
        sysPowers.setCreateTime(DateUtils.getNowDate());
        return sysPowersMapper.insertSysPowers(sysPowers);
    }

    /**
     * 修改职权信息
     * 
     * @param sysPowers 职权信息
     * @return 结果
     */
    @Override
    public int updateSysPowers(SysPowers sysPowers)
    {

        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        sysPowers.setUpdateBy(sysDept.getDeptName());
        sysPowers.setUpdateTime(DateUtils.getNowDate());
        return sysPowersMapper.updateSysPowers(sysPowers);
    }

    /**
     * 批量删除职权信息
     * 
     * @param recNos 需要删除的职权信息ID
     * @return 结果
     */
    @Override
    public int deleteSysPowersByIds(String[] recNos)
    {
        int i=0;
        for (String recNo : recNos) {
            SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
            SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
            SysPowers sysPowers = new SysPowers();
            sysPowers.setUpdateBy(sysDept.getDeptName());
            sysPowers.setUpdateTime(DateUtils.getNowDate());
            sysPowers.setRecNo(recNo);
            sysPowersMapper.deleteSysPowersById(recNo);
            i++;
        }
        return i;
    }

    /**
     * 删除职权信息信息
     * 
     * @param recNo 职权信息ID
     * @return 结果
     */
    @Override
    public int deleteSysPowersById(String recNo)
    {
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        SysPowers sysPowers = new SysPowers();
        sysPowers.setUpdateBy(sysDept.getDeptName());
        sysPowers.setUpdateTime(DateUtils.getNowDate());
        return sysPowersMapper.deleteSysPowersById(recNo);
    }
}
